package com.itlaobing.service.impl;

import com.itlaobing.dao.IAccountDao;
import com.itlaobing.model.Account;
import com.itlaobing.service.IAccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Classname AccountServiceImpl
 * @Description TODO()
 * @Date 2020/9/15 14:23
 * @Author by Administrator
 * @Version v1.0
 */
@Service
@Transactional(readOnly = false, propagation = Propagation.SUPPORTS)
public class AccountServiceImpl implements IAccountService {

    @Resource
    private IAccountDao accountDao;


    @Override
    @Transactional
    public void transfer(Integer sourceId, Integer targetId, float money) {
        Account source = accountDao.findById(sourceId);
        Account target = accountDao.findById(targetId);

        source.setMoney(source.getMoney() - money);
        target.setMoney(target.getMoney() + money);

        accountDao.update(source);
        // 模拟转账失败异常
        int i = 1 / 0;
        accountDao.update(target);
    }
}
